#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <locale.h>
#include "mtask.h"
/* set message task */
int mtask_set(MTASK *mtask, char *ip, int port, int mtaskid, int qtaskid)
{
    int ret = -1;
    if(mtask && ip && port > 0 && mtaskid > 0 && qtaskid > 0)
    {
        mtask->mtaskid = mtaskid;
        mtask->qtaskid = qtaskid;
        mtask->rsa.sin_family = AF_INET;
        mtask->rsa.sin_addr.s_addr = inet_addr(ip);
        mtask->rsa.sin_port = htons(port);
        ret = 0;
    }
    return ret;
}

/* connect to qtask */
int mtask_connect(MTASK *mtask)
{
    if(mtask)
    {
        if(mtask->fd <= 0 && (mtask->fd = socket(AF_INET, SOCK_STREAM, 0)) <= 0)
        {
            fprintf(stderr, "create socket() failed, %s\n", strerror(errno));
            return -2;
        }
        if(connect(mtask->fd, (struct sockaddr *)&(mtask->rsa), sizeof(struct sockaddr_in)) != 0)
        {
            fprintf(stderr, "connect() failed, %s\n", strerror(errno));
            close(mtask->fd);
            mtask->fd = -1;
            return -3;
        }
        return 0;
    }
    return -1;
}

/* push data id and get new data_id */
int64_t mtask_commit(MTASK *mtask, int64_t data_id, int flag)
{
    int64_t id = -1;
    MHEAD mhead = {0};

    if(mtask)
    {
        if(mtask->fd <= 0 && mtask_connect(mtask) <= 0) 
            return -2;
        mhead.cmd = MTASK_CMD_POP;
        if(data_id > 0) 
        {
            mhead.cmd |= MTASK_CMD_PUSH;
            mhead.data_id = data_id;
        }
        mhead.flag = flag;
        mhead.mtaskid = mtask->mtaskid;
        mhead.mtaskid = mtask->qtaskid;
        if(write(mtask->fd, &mhead, sizeof(MHEAD)) <= 0)
        {
            fprintf(stderr, "write task failed, %s\n", strerror(errno));
            shutdown(mtask->fd, SHUT_RD|SHUT_WR);
            close(mtask->fd);
            mtask->fd = -1;
            return -3;
        }
        if(read(mtask->fd, &mhead, sizeof(MHEAD)) <= 0)
        {
            fprintf(stderr, "read task failed, %s\n", strerror(errno));
            shutdown(mtask->fd, SHUT_RD|SHUT_WR);
            close(mtask->fd);
            mtask->fd = -1;
            return -4;
        }
        id = mhead.data_id;
    }
    return id;
}

/* close message task */
void mtask_close(MTASK *mtask)
{
    if(mtask->fd)
    {
        shutdown(mtask->fd, SHUT_RD|SHUT_WR);
        close(mtask->fd);
        mtask->fd = -1;
    }
    return ;
}

#ifdef _DEBUG_MTASK
int main()
{
    int64_t data_id = 0, id = 0;
    MTASK mtask = {0};
    int i = 0;

    if(mtask_set(&mtask, "127.0.0.1", 6666, 1, 2) == 0
            && mtask_connect(&mtask) == 0)
    {
        while(i < 100000 && (id = mtask_commit(&mtask, data_id, 0)) >= 0)
        {
            fprintf(stdout, "%d:{data_id:%lld new_id:%lld}\n", i, (long long)data_id, (long long)id);
            data_id = id;
            ++i;
        }
        mtask_close(&mtask);
    }
    return 0;
}
//gcc -o mtask mtask.c -D_DEBUG_MTASK && ./mtask
#endif
